﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using AutoMapper;

using NJ.Common.Extension;
using NJ.Dto.RoleMenu;
using NJ.Entity;

namespace NJ.Service.Menu
{
    public class MenuService : BaseService, IMenuService
    {
        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <returns></returns>
        public async Task<List<MenuDto>> GetMenusAsync()
        {
            var data = await GetAll<MenuEntity>().ProjectToListAsync<MenuDto>();
            return data;
        }
        /// <summary>
        /// 获取一个菜单
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<MenuDto> GetMenuAsync(int Id)
        {
            var menuDto = await GetAll<MenuEntity>(t => t.Id == Id).ProjectToFirstOrDefaultAsync<MenuDto>();
            return menuDto;
        }
        /// <summary>
        /// 获取父级菜单
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<List<MenuDto>> GetParentMenusAsync(int Id)
        {
            var list = new List<MenuDto>();

            var entity = await FindAsync<MenuEntity>(Id);

            if (entity != null)
            {
                var ids = new List<int>();
                var array = entity.Leader.Split('-');
                foreach (var item in array)
                {
                    if (item.To<int>() > 0)
                    {
                        ids.Add(item.To<int>());
                    }
                }

                ids.Add(Id);

                list = await GetAll<MenuEntity>(t => ids.Contains(t.Id)).ProjectToListAsync<MenuDto>();
            }
            else
            {
                list = await Task.FromResult(new List<MenuDto>());
            }

            return list;
        }

        /// <summary>
        /// 保存菜单
        /// </summary>
        /// <param name="menuDto"></param>
        /// <returns></returns>
        public async Task<int> SaveMenuAsync(CreateOrUpdateMenuDto input)
        {
            var menuEntity = await FindAsync<MenuEntity>(input.Id);
            if (menuEntity == null)
            {
                var leader = "0";
                var menu = await FindAsync<MenuEntity>(input.PId);
                if (menu != null)
                {
                    leader = menu.Leader + "-" + menu.Id;
                }
                menuEntity = new MenuEntity
                {
                    Icon = input.Icon,
                    Leader = leader,
                    PId = input.PId,
                    Title = input.Title,
                    Url = input.Url
                };
                Add(menuEntity);
            }
            else
            {
                menuEntity.Icon = input.Icon;
                menuEntity.Title = input.Title;
                menuEntity.Url = input.Url;
                Update(menuEntity);
            }
            int result = await SaveChangesAsync();
            return result;
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<int> DeleMenuAsync(int Id)
        {
            var entity = await FindAsync<MenuEntity>(Id);

            var leader = entity.Leader + "-" + Id;

            var query = GetAll<MenuEntity>(t => t.Leader.StartsWith(leader) || t.Id == Id);
            var query_rm = GetAll<RoleMenuEntity>(t => query.Any(m => m.Id == t.Id));

            DeleteBatch(query);
            DeleteBatch(query_rm);

            var result = await SaveChangesAsync();

            return result;
        }
    }
}